package com.example.seey2023.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.seey2023.entity.HeSuan;
import com.example.seey2023.entity.Role;
import com.example.seey2023.entity.User;
import com.example.seey2023.entity.Vaccine;
import com.example.seey2023.service.VaccineService;
import com.example.seey2023.vo.DataView;
import com.example.seey2023.vo.HeSuanVo;
import com.example.seey2023.vo.VaccineVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/vaccine")
public class VaccineController {
    @Autowired
    private VaccineService vaccineService;

    @RequestMapping("/toVaccine")
    public String toVaccine() {
        return "vaccine/vaccine";
    }

//    @RequestMapping("/toVaccineReport")
//    public String toVaccineReport() { return  "admin/vaccinereport";}

    @RequestMapping("/loadAllVaccine")
    @ResponseBody
    public DataView loadAllVaccine(VaccineVo vaccineVo) {
        IPage<Vaccine> page = new Page<>(vaccineVo.getPage(), vaccineVo.getLimit());
        QueryWrapper<Vaccine> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vaccineVo.getName()),"name",vaccineVo.getName());
        vaccineService.page(page, queryWrapper);
        return new DataView(page.getTotal(), page.getRecords());
    }

    @RequestMapping("/loadOneVaccine")
    @ResponseBody
    public DataView loadOneVaccine(VaccineVo vaccineVo, HttpServletRequest httpServletRequest) {
        IPage<Vaccine> page = new Page<>(vaccineVo.getPage(), vaccineVo.getLimit());
        HttpSession session = httpServletRequest.getSession();
        User user = (User) session.getAttribute("user");
        String username = null;

        if (Objects.nonNull(user) ){
            username = user.getUsername();
        }
        LambdaQueryWrapper<Vaccine> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Vaccine> eq = objectLambdaQueryWrapper.eq(Vaccine::getName, username);
        vaccineService.page(page, eq);
        return new DataView(page.getTotal(), page.getRecords());
    }

    @RequestMapping("/addVaccine")
    @ResponseBody
    public DataView addVaccine(Vaccine vaccine){
        vaccineService.save(vaccine);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("新冠疫苗接种记录添加成功！");
        return dataView;
    }

    @RequestMapping("/updateVaccine")
    @ResponseBody
    public DataView updateVaccine(Vaccine vaccine){
        vaccineService.updateById(vaccine);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("新冠疫苗接种记录修改成功！");
        return dataView;
    }

    @RequestMapping("/deleteVaccine")
    @ResponseBody
    public DataView deleteVaccine(Integer id){
        vaccineService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("新冠疫苗接种记录删除成功！");
        return dataView;
    }

    @RequestMapping("/excelImportVaccine")
    @ResponseBody
    public DataView excelImportVaccine(@RequestParam("file") MultipartFile file) throws IOException {
        DataView dataView = new DataView();
        // 1.文件不为空
        if (file.isEmpty()) {
            dataView.setMsg("文件为空，不能上传！");
        }
        // 2.POI获取Excel解析数据
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = wb.getSheetAt(0);
        // 3.定义一个程序集合 接受 文件中的数据
        List<Vaccine> list = new ArrayList<>();
        XSSFRow row = null;
        // 4.解析数据，装到集合里面
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            // 4.1 定义实体
            Vaccine vaccine = new Vaccine();
            // 4.2 每一行的数据，放到实体类里面
            row = sheet.getRow(i);
            // 4.3 解析数据
            XSSFCell cell2 = row.getCell(2);
            XSSFCell cell3 = row.getCell(3);
            cell2.setCellType(CellType.STRING);
            cell3.setCellType(CellType.STRING);

            vaccine.setName(row.getCell(0).getStringCellValue());
            vaccine.setAge((int) row.getCell(1).getNumericCellValue());
            vaccine.setPhone(cell2.getStringCellValue());
            vaccine.setCard(cell3.getStringCellValue());
            vaccine.setZhenci(row.getCell(4).getStringCellValue());
            vaccine.setPici(row.getCell(5).getStringCellValue());
            vaccine.setCompany(row.getCell(6).getStringCellValue());
            vaccine.setDanwei(row.getCell(7).getStringCellValue());
            vaccine.setCreateTime(row.getCell(8).getDateCellValue());
            // 5.添加list集合
            list.add(vaccine);
        }
        // 6. 插入数据库
        vaccineService.saveBatch(list);
        dataView.setCode(200);
        dataView.setMsg("excel插入成功！");
        return dataView;
    }
}
